<%@page import="java.text.DecimalFormat"%>
<%@page import="java.util.Map"%>
<%@page import="com.winexpress.order.OrderLine"%>
<%@page import="java.util.List"%>
<%@page import="com.winexpress.order.OrderHistory"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>WineXpress Transaction Result</title>
    </head>
    <body>
        <t:customerNavigation/>
        <br>

        <div style="width: 80%; margin: auto; border: solid;">
            <h2 align="center">WineXpress Order</h2>
            <% OrderHistory oh = (OrderHistory) request.getAttribute("history");
                boolean ohID = (Boolean)request.getAttribute("ohID");%>
            <% if (oh == null) {%>
            <h4 align="center">Sorry, the selected wines are out of stock currently.</h4>
            <%} else if (!oh.isIsClosed()) {%>
            <h4 align="center">Sorry, the transaction fails. Please try again.</h4>
            <%} else {%>
            <p align="left"><h3>&nbsp;Dear <% out.print(oh.getCustomer().getFirstName() + " " + oh.getCustomer().getLastName());%>,</h3></p>
        <br/>
        <p align="left"><h4>&nbsp;The transaction is completed successfully. Here is the detail of the transaction.</h4></p>
    <br/>

    <p class="bg-info" style="font-size: 16px;">&nbsp;Payment Details</p>
    <table class="table table-striped table-bordered" style="text-align: left;">
        <tbody>
            <tr style="height: 10%">
                <td style="width: 25%; vertical-align: middle;">PayPal Transaction ID: </td>
                <td><% out.print(request.getAttribute("TransactionID"));%></td>
            </tr>
            <tr style="height: 10%">
                <% String temp = oh.getCreditCardNumber();
                    String cardNum = temp.substring(0, 4);
                    for (int i = 4; i < temp.length() - 2; i++) {
                        cardNum = cardNum + "*";
                    }
                    cardNum = cardNum + temp.substring(temp.length() - 2);%>
                <td style="width: 25%; vertical-align: middle;">Credit Card Number: </td>
                <td><%out.print(cardNum);%></td>
            </tr>
            <tr style="height: 10%">
                <td style="width: 25%; vertical-align: middle;">Payment Amount: </td>
                <td><% out.print("S$ " + request.getAttribute("Amount"));%></td>
            </tr>
        </tbody>
    </table>

    <p class="bg-info" style="font-size: 16px;">&nbsp;Shipping Details</p>
    <table class="table table-striped table-bordered" style="text-align: left;">
        <tbody>
            <tr style="height: 10%">
                <td style="width: 25%; vertical-align: middle;">Shipping To: </td>
                <td><%out.print(oh.getShippingAddress());%></td>
            </tr>
        </tbody>
    </table>

    <p class="bg-info" style="font-size: 16px;">&nbsp;Order Details:</p>
    <p style="font-size: 16px;"> &nbsp;WineXpress OrderID: <% out.print(oh.getId()); %></p>
    <table border="0" class="table table-striped">
        <tr>
            <th style="width: 350px;">Wine</th>
            <th style="text-align: center;">Qty</th>
            <th style="text-align: right;">Price</th>
            <th style="text-align: right;">Discount</th>
            <th style="text-align: right;">Single Price</th>
            <th style="text-align: right;">Final Price</th>
        </tr>
        <% List<OrderLine> ols = oh.getOrderLines(); %>
        <% double sum = 0.0; %>
        <% for (OrderLine ol : ols) {%>
        <tr>
            <td><% out.print(ol.getWine().getName());%></td>
            <td style="text-align: center;"><% out.print(ol.getQuantity());%></td>
            <td style="text-align: right;"><% out.print(ol.getPrice()); %></td>
            <td style="text-align: right;"><% out.print(ol.getDiscount());%></td>
            <td style="text-align: right;"><% out.print(ol.getPrice() * (100 - ol.getDiscount()) / 100);%></td>
            <td style="text-align: right;"><% out.print(ol.getQuantity() * ol.getPrice() * (100 - ol.getDiscount()) / 100);%></td>
            <% sum += ol.getQuantity() * ol.getPrice() * (100 - ol.getDiscount()) / 100; %>
        </tr>
        <%}%>
        <tr>
            <td colspan="5" align="right"><b>Order Discount:</td>
            <td align="right"><% out.print(oh.getOrderDiscount());%></td>
        </tr>
        <tr>
            <td colspan="5" align="right"><b>Order Sum:</td>
            <% DecimalFormat df = new DecimalFormat("#.0");%>
            <td align="right"><% out.print(df.format(sum));%></td>
        </tr>
    </table>

    <SCRIPT LANGUAGE="JavaScript">
        if (window.print) {
            document.write('<form><button type="button" onClick="window.print()" style="margin-left: 42%"><h4><b>Print the order</b></h4></button></form>');
        }
    </script>
    <%}%>
</div>

<t:footer/>
</body>
</html>
